/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";

let token = sessionStorage.getItem('supplier_token');
let changePassword = {
    init: function(){
        this.setUserName();
        $('.changepw-form').validate({
            errorElement: 'label', //default input error message container
            errorClass: 'help-inline', // default input error message class
            focusInvalid: false, // do not focus the last invalid input
            rules: {
                passWd: {
                    required: true
                },
                nPassWd: {
                    required: true
                },
                rnPassWd: {
                    equalTo: "#nPassWd"
                }
            },

            messages: {
                passWd: {
                    required: "请输入原密码"
                },
                password: {
                    required: "请填写密码"
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit
                $('.alert-error', $('.login-form')).show();
            },

            highlight: function (element) { // hightlight error inputs
                $(element)
                    .closest('.control-group').addClass('error'); // set error class to the control group
            },

            success: function (label) {
                label.closest('.control-group').removeClass('error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                let $form = $(form);
                let username = sessionStorage.getItem('username');
                let passWd = $form.find('#passWd').val();
                let nPassWd = $form.find('#nPassWd').val();
                var data = {
                    "usrNam": username,
                    "mim": passWd,
                    "npasswd": nPassWd
                }
                console.log(data);

                $.ajax({
                    url: HOST_ADDR + '/cpmp/splrSlfMng/mim',
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    headers: {
                        'access_token': token,
                        'login_type': 'splr'
                    },
                    contentType:"application/json",
                    success: function(resp){
                        if(resp.success){
                            alert('修改密码成功');
                            sessionStorage.setItem('supplier_token','');
                            window.location.href='/supplier_self/login.html';
                        }
                        else{
                            alert(resp.msgInf);
                        }
                    },
                    error:function(err){
                        console.log(err);
                    }

                })
            }
        });
    },
    setUserName(){
        let username = sessionStorage.getItem('username');
        $('.J_username').html(username);
    }
}
jQuery(document).ready(function() {
    App.init();
    changePassword.init();
});
